package com.zero.youxiao.service;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zero.basic.exception.SmsErrorConstant;
import com.zero.basic.exception.SmsRuntimeException;
import com.zero.basic.exception.SmsStatusCode;
import com.zero.mms.model.SalaryCalItem;
import com.zero.mms.model.SalaryItem;
import com.zero.youxiao.constant.SalaryItemConstant;
import com.zero.youxiao.dao.SalaryCalItemDao;
import com.zero.youxiao.dao.SalaryItemDao;

@Service("salaryItemService")
public class SalaryItemService {

	@Inject
	private SalaryItemDao salaryItemDao;

	@Inject
	private SalaryCalItemDao salaryCalItemDao;

	@Transactional(rollbackFor = Exception.class)
	public List<SalaryItem> searchSalaryItems(String itemName, String itemType)
			throws SmsRuntimeException {
		try {
			if (itemType == null)
				itemType = "";
			if (itemName == null)
				itemName = "";
			List<SalaryItem> salaryItems = salaryItemDao.getSalaryItems(
					itemName, itemType);
			return salaryItems;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,
					SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}
	
	@Transactional(rollbackFor = Exception.class)
	public void addSalaryItem(String itemName, String itemType,
			Integer lastOrder,Integer itemOrder, String itemAdd, String itemShow,
			String itemMemo, Integer itemCalItemId, String itemCalPar1,
			String itemOp, String itemCalPar2) throws SmsRuntimeException {
		try {
			if(lastOrder != itemOrder)
				salaryItemDao.updateOrder(itemOrder,lastOrder);
			if("".equals(itemName) || "".equals(itemType) || "".equals(itemAdd) ||"".equals(itemShow))
				throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE, SmsErrorConstant.INVALID_AUGUMENT, "参数不合法");
			SalaryItem salaryItem = new SalaryItem();
			salaryItem.setSalaryItemName(itemName);
			salaryItem.setSalaryItemType(itemType);
			salaryItem.setSalaryOrder(itemOrder);
			salaryItem.setSalaryAddItem(itemAdd);
			salaryItem.setShow(itemShow);
			salaryItem.setSalaryMemo(itemMemo);
			if (itemType.equals(SalaryItemConstant.Salary_cal)) {
				if("".equals(itemCalPar1) || "".equals(itemCalPar2) || "".equals(itemOp))
					throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE, SmsErrorConstant.INVALID_AUGUMENT, "计算公式不合法");
				String calGongshi = itemCalPar1 + itemOp + itemCalPar2;
				salaryItem.setSalaryCal(calGongshi);
			}
			salaryItemDao.add(salaryItem);
			if (itemType.equals(SalaryItemConstant.Salary_cal)) {
				SalaryCalItem salaryCalItem = new SalaryCalItem();
				salaryCalItem.setCalItem(itemCalItemId);
				salaryCalItem.setOperator(itemOp);
				salaryCalItem.setSeNum(Double.valueOf(itemCalPar2));
				salaryCalItem.setSalaryItemId(salaryItem.getSalaryItemId());
				salaryCalItemDao.add(salaryCalItem);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,
					SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}

	@Transactional(rollbackFor = Exception.class)
	public void updateSalaryItem(Integer itemId, String itemName,
			String itemType, Integer itemOrder, String itemAdd,
			String itemShow, String itemMemo, Integer itemCalItemId,
			String itemCalPar1, String itemOp, String itemCalPar2)
			throws SmsRuntimeException {
		try {
			SalaryItem salaryItem = salaryItemDao.load(itemId);
			salaryItem.setSalaryItemName(itemName);
			salaryItem.setSalaryItemType(itemType);
			salaryItem.setSalaryOrder(itemOrder);
			salaryItem.setSalaryAddItem(itemAdd);
			salaryItem.setShow(itemShow);
			salaryItem.setSalaryMemo(itemMemo);
			if (itemType.equals(SalaryItemConstant.Salary_cal)) {
				String calGongshi = itemCalPar1 + itemOp + itemCalPar2;
				salaryItem.setSalaryCal(calGongshi);
			}
			salaryItemDao.update(salaryItem);
			if (itemType.equals(SalaryItemConstant.Salary_cal)) {
				SalaryCalItem salaryCalItem = salaryCalItemDao
						.getByItemId(itemId);
				boolean exist = true;
				if (salaryCalItem == null) {
					salaryCalItem = new SalaryCalItem();
					exist = false;
				}
				salaryCalItem.setCalItem(itemCalItemId);
				salaryCalItem.setOperator(itemOp);
				salaryCalItem.setSeNum(Double.valueOf(itemCalPar2));
				salaryCalItem.setSalaryItemId(salaryItem.getSalaryItemId());
				if (!exist)
					salaryCalItemDao.add(salaryCalItem);
				else
					salaryCalItemDao.update(salaryCalItem);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,
					SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}

	@Transactional(rollbackFor = Exception.class)
	public void deleteItem(Integer itemId) throws SmsRuntimeException {
		try {
			if ("".equals(itemId) || itemId == null)
				throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,
						SmsErrorConstant.INVALID_AUGUMENT, "item不存在");
			salaryItemDao.delete(itemId);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,
					SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}

	@Transactional(rollbackFor = Exception.class)
	public void updateCal(Integer itemId, Integer itemCalItemId,
			String itemCalPar1, String itemOp, String itemCalPar2)
			throws SmsRuntimeException {
		try {
			if ("".equals(itemId) || itemId == null)
				throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,
						SmsErrorConstant.INVALID_AUGUMENT, "item不存在");

			SalaryCalItem salaryCalItem = salaryCalItemDao.load(itemId);
			salaryCalItem.setCalItem(itemCalItemId);
			salaryCalItem.setOperator(itemOp);
			salaryCalItem.setSeNum(Double.valueOf(itemCalPar2));
			salaryCalItemDao.update(salaryCalItem);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,
					SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}

	@Transactional(rollbackFor = Exception.class)
	public Integer getOrderList() throws SmsRuntimeException {
		try {
			return salaryItemDao.getOrderList().intValue() + 1;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,
					SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}

	@Transactional(rollbackFor = Exception.class)
	public List<SalaryItem> getAllParSalaryItems() throws SmsRuntimeException {
		try {
			return salaryItemDao.getAllParItems();
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,
					SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}

}
